/**
 * 单例模式：保证一个类仅有一个实例，并提供一个访问它的全局访问点
 * 要满足单一职责原则(Single Responsibility Principle)
 * 一个函数只负责一个功能
 */

var Singleton = function (name) {
  this.name = name;
};

Singleton.prototype.getName = function () {
  console.log(this.name);
};

// 代理
var proxySingleton = (function () {
  var instance;
  return function (name) {
    if (!instance) {
      instance = new Singleton(name);
    }
    return instance;
  };
})();

var a = proxySingleton("a");
var b = proxySingleton("b");

// console.log(a === b);

// 通用的惰性单例
var getSingle = function (fn) {
  var result;
  return function () {
    return result || (result = fn.apply(this, arguments));
  };
};

var createLayer = function () {
  console.log("创建弹窗");
};

var createSingleLayer = getSingle(createLayer);
var loginer1 = createSingleLayer();
var loginer2 = createSingleLayer();

console.log(loginer1 === loginer2);
